package com.ccsu.joysport.controller;


import com.ccsu.joysport.entity.Results;
import com.ccsu.joysport.service.ResultService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;


import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/ccsu/user")
public class getScoreController {
    @Autowired
    private ResultService resultService;
//收到token 返回成绩
    @GetMapping("/getScore")
    public ResponseEntity<List<Map<String, String>>> getScore(@RequestParam String token) {
        List<Results> results = resultService.getResultsByToken(token);
        if (results != null && !results.isEmpty()) {
            List<Map<String, String>> responseList = new ArrayList<>();
            for (Results result : results) {
                Map<String, String> response = new HashMap<>();
                response.put("weight", result.getWeight());
                response.put("height", result.getHeight());
                response.put("vitalcapacity", result.getVitalcapacity());
                response.put("rate", result.getRate());
                response.put("StudentNumber", result.getStudentNumber());
                responseList.add(response);
            }
            return ResponseEntity.ok(responseList);//如有多个结果全部返回
        } else {
            return ResponseEntity.status(HttpStatus.NOT_FOUND).body(null);
        }
    }
}
